#include <stdio.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include "rtcfun.h"
#include "apphead.h"

#define RTC_TICK_ON      _IO('p', 0x03)  /* Update int. enable on        */
#define RTC_TICK_OFF     _IO('p', 0x04)  /* ... off                      */
#define RTC_TICK_SET      _IO('p', 0x05)  /* Periodic int. enable on      */
#define RTC_TICK_READ     _IO('p', 0x06)  /* ... off                      */

void system_rtc_sync(void)
{
    int fd;
	int retval;
	struct rtc_time rtc_tm;
	struct tm *date;

	fd = open ("/dev/rtc0", O_RDONLY);
    if(fd < 0)
	{
	  printf("/dev/rtc0 open fail !!!\n");
      return ;
	}
	system_time_get(&date);
	rtc_tm.tm_year = date->tm_year;
	rtc_tm.tm_mon = date->tm_mon;
	rtc_tm.tm_mday = date->tm_mday;
	rtc_tm.tm_hour = date->tm_hour;
	rtc_tm.tm_min = date->tm_min;
	rtc_tm.tm_sec = date->tm_sec;
  
	retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
	if (retval <0) {
		printf("ioctl RTC_SET_TIME  faild!!!\n");
		return ;
	}

	/*print current time*/
	printf("Adjust current RTC time as: %04d-%02d-%02d %02d:%02d:%02d\n\n",
	rtc_tm.tm_year + 1900,
	rtc_tm.tm_mon + 1,
	rtc_tm.tm_mday,
	rtc_tm.tm_hour,
	rtc_tm.tm_min,
	rtc_tm.tm_sec);
	printf("Adjust current RTC time test OK!\n");
}	


